package com.light.gulimall.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.light.common.dto.savespuinfo.MemberPrice;
import com.light.common.dto.savespuinfo.SaveSpuInfoDTO;
import com.light.common.dto.savespuinfo.Skus;
import com.light.common.utils.PageUtils;
import com.light.common.utils.Query;
import com.light.common.utils.R;
import com.light.gulimall.coupon.dao.*;
import com.light.gulimall.coupon.entity.*;
import com.light.gulimall.coupon.service.CouponService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;


@Slf4j
@Service("couponService")
public class CouponServiceImpl extends ServiceImpl<CouponDao, CouponEntity> implements CouponService {

    @Autowired
    private CouponService couponService;

    /**
     * sms_spu_bounds 商品spu积分设置
     */
    @Autowired
    private SpuBoundsDao spuBoundsDao;

    /**
     * sms_sku_ladder 商品阶梯价格
     */
    @Autowired
    private SkuLadderDao skuLadderDao;

    /**
     * sms_sku_full_reduction 商品满减信息
     */
    @Autowired
    private SkuFullReductionDao fullReductionDao;

    /**
     * sms_member_price 商品会员价格
     */
    @Autowired
    private MemberPriceDao memberPriceDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CouponEntity> page = this.page(
                new Query<CouponEntity>().getPage(params),
                new QueryWrapper<CouponEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * product模块远程调用  保存积分优惠相关信息
     *
     * @param skuInfo product模块传递信息
     * @return 是否成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R saveCouponReleaseInfo(SaveSpuInfoDTO skuInfo) {
        try {
            //4.保存sms_spu_bounds coupon模块
            couponService.saveSpuBonds(skuInfo);

            couponService.saveLadderAndReduce(skuInfo);
        } catch (Exception e) {
            log.error("保存积分优惠相关信息发生错误！",e);
            return R.error(500,"保存积分优惠相关信息发生错误");
        }
        return R.ok();
    }

    public void saveLadderAndReduce(SaveSpuInfoDTO skuInfo) {
        List<Skus> skus = skuInfo.getSkus();
        if (!CollectionUtils.isEmpty(skus)) {
            for (Skus sku : skus) {
                //9.保存sms_sku_ladder coupon模块
                SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
                BeanUtils.copyProperties(sku, skuLadderEntity);
                Long skuId = sku.getSkuId();
                skuLadderEntity.setSkuId(skuId);
                skuLadderDao.insert(skuLadderEntity);
                //10.保存sms_sku_full_reduction  coupon模块
                SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
                BeanUtils.copyProperties(sku, skuFullReductionEntity);
                skuFullReductionEntity.setSkuId(skuId);
                fullReductionDao.insert(skuFullReductionEntity);
                //11.保存sms_member_price  coupon模块
                List<MemberPrice> memberPrice = sku.getMemberPrice();
                if (!CollectionUtils.isEmpty(memberPrice)) {
                    for (MemberPrice price : memberPrice) {
                        MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                        BeanUtils.copyProperties(price, memberPriceEntity);
                        memberPriceEntity.setSkuId(skuId);
                        memberPriceDao.insert(memberPriceEntity);
                    }
                }
            }
        }
    }


    public void saveSpuBonds(SaveSpuInfoDTO skuInfo) {
        Long spuId = skuInfo.getSpuId();
        SpuBoundsEntity spuBoundsEntity = new SpuBoundsEntity();
        BeanUtils.copyProperties(skuInfo, spuBoundsEntity);
        spuBoundsEntity.setBuyBounds(skuInfo.getBounds().getBuyBounds());
        spuBoundsEntity.setGrowBounds(skuInfo.getBounds().getGrowBounds());
        spuBoundsEntity.setSpuId(spuId);
        spuBoundsDao.insert(spuBoundsEntity);
    }

}